﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Zor_IDAL;
using Zor_Modes;

namespace Zor_DAL
{
    public class CredentialInfo_DAL
    {
        /// <summary>
        /// 添加房屋证件信息
        /// </summary>
        /// <param name="ci"></param>
        /// <returns></returns>
        public static BaseAnswer InserCredentialInfo(CredentialInfo ci)
        {
            var sql = @"insert into CredentialInfo(Property_Id,Property_Card,Property_IDNumber,Property_LeaseContract,Property_PaymentCertificate) values(@Property_Id,@Property_Card,@Property_IDNumber,@Property_LeaseContract,@Property_PaymentCertificate)";
            BaseAnswer bar = DapperEdit<FlowInfo>.DoEdit(sql, ci);
            return bar;
        }

        /// <summary>
        /// 根据房屋ID获取信息集合
        /// </summary>
        /// <returns></returns>
        public static List<CredentialInfo> GetCredentialInfoList(CredentialInfo ci)
        {
            var sql = @" select * from CredentialInfo where Property_Id=@Property_Id ";
            List<CredentialInfo> lst = DapperQuery<CredentialInfo>.DoQueryList(sql, ci);
            return lst;
        }

        /// <summary>
        /// 添加房屋证件信息
        /// </summary>
        /// <param name="ci"></param>
        /// <returns></returns>
        public static BaseAnswer UpdateCredentialInfo(CredentialInfo ci)
        {
            var sql = @"update CredentialInfo set ";
            if (!string.IsNullOrEmpty(ci.Property_IDNumber)) 
            {
                sql += " Property_IDNumber=@Property_IDNumber ";
            }
            if (!string.IsNullOrEmpty(ci.Property_LeaseContract))
            {
                sql += " Property_LeaseContract=@Property_LeaseContract ";
            }
            if (!string.IsNullOrEmpty(ci.Property_PaymentCertificate))
            {
                sql += " Property_PaymentCertificate=@Property_PaymentCertificate ";
            }
            if (!string.IsNullOrEmpty(ci.Property_Card))
            {
                sql += " Property_Card=@Property_Card ";
            }
            sql += " where Property_Id=@Property_Id ";
            BaseAnswer bar = DapperEdit<FlowInfo>.DoEdit(sql, ci);
            return bar;
        }


        /// <summary>
        /// 审批
        /// </summary>
        /// <param name="pi"></param>
        /// <returns></returns>
        public static BaseAnswer ApplyPushMeg(CredentialInfo ci)
        {
            var sql = @"update CredentialInfo set AuditStatus = @AuditStatus,AuditMeg = @AuditMeg,AuditUserId=@AuditUserId,AuditTime=@AuditTime where Credential_Id = @Credential_Id";
            BaseAnswer bar = DapperEdit<FlowInfo>.DoEdit(sql, ci);
            return bar;
        }

        /// <summary>
        /// 获取推送消息集合，根据用户权限
        /// </summary>
        /// <returns></returns>
        public static List<CredentialInfo> GetPushMegList(PropertyInfo pi)
        {
            List<CredentialInfo> lst = new List<CredentialInfo>();
            var sql = @" select ci.*,us.User_RealName as AuditUserName
                        from CredentialInfo ci
                        left join Users us on ci.AuditUserId=us.User_Id ";
            if (pi.Dt_ParentId != 0)
            {
                sql += " where us.Dt_ParentId=@Dt_ParentId ";
                lst = DapperQuery<CredentialInfo>.DoQueryList(sql, pi);
            }
            else
            {
                lst = DapperQuery<CredentialInfo>.DoQueryList(sql, null);
            }
            return lst;
        }
    }
}
